package com.purong.purong.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.purong.purong.R;
import com.purong.purong.bean.CreditCardBean;
import com.purong.purong.util.CommonUtil;
import com.purong.purong.widget.RadiusImageView;
import com.yao.sdk.glide.GlideUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Circle Photo 列表的数据适配器
 */
public class HomeCreditcardAdapter extends RecyclerView.Adapter<HomeCreditcardAdapter.ViewHolder> {


    private List<CreditCardBean> creditCardList = new ArrayList<>();

    private Context mContext;
    private OnCreditClickListener mListener;
    public HomeCreditcardAdapter(Context context) {
        this.mContext = context;
    }
    public void setOnCreditClickListener(OnCreditClickListener listener){
        this.mListener=listener;
    }
    public void setData(List<CreditCardBean> creditCardList) {
//        if (Utils.isEmpty(imageList)) {
//            return;
//        }
        this.creditCardList = creditCardList;
    }


    /**
     * 通过ViewHolder可以访问每个item的所有控件
     */
    public static class ViewHolder extends RecyclerView.ViewHolder {
        //绑定每个CircleItem的控件引用到ViewHolder
        private RadiusImageView img;
        private RelativeLayout mainLay;
        private TextView name,outline,applyNum;
        public ViewHolder(View v) {
            super(v);
            img=(RadiusImageView)v.findViewById(R.id.creditcard_img);
            name=(TextView)v.findViewById(R.id.creditcard_name);
            outline=(TextView)v.findViewById(R.id.credit_outline);
            applyNum=(TextView)v.findViewById(R.id.credit_apply_num);
            mainLay=(RelativeLayout)v.findViewById(R.id.main_lay);



        }
    }


    // 创建每个Item的视图 (invoked by the item_circle manager)
    @Override
    public HomeCreditcardAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                               int viewType) {
        //创建circleItem所对应的控件
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.creditcard_item, parent, false);
        // set the view's size, margins, paddings and item_circle parameters
        ViewHolder vh = new ViewHolder(v);

        return vh;
    }

    // Replace the contents of a view (invoked by the item_circle manager)
    @Override
    public void onBindViewHolder(final ViewHolder mHolder, final int position) {
        mHolder.mainLay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener!=null){
                    mListener.onCreditClick(creditCardList.get(position));
                }
            }
        });
        mHolder.name.setText(creditCardList.get(position).getCredit_product_name());
        mHolder.outline.setText(creditCardList.get(position).getCredit_product_outline());
        mHolder.applyNum.setText(CommonUtil.numConvert(Integer.valueOf(creditCardList.get(position).getCredit_apply_num()),1));
        GlideUtil.getInstance().load(mContext, creditCardList.get(position).getCredit_product_url(), mHolder.img, true);
    }



    // 获取Circle数据的item个数
    @Override
    public int getItemCount() {
        return creditCardList.size();
    }

    public interface OnCreditClickListener{
        void onCreditClick(CreditCardBean card);
    }
}


